package com.zd.user.exception;

import com.zd.common.bean.ZdNovelResponse;
import com.zd.common.enums.ResponseStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class RunExceptions {

    /**
     * 拦截用户注册用户名重复,返回异常信息
     * @param exception
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public ZdNovelResponse fail(Exception exception) {
        //拦截注册时输入已有用户名
        if (exception instanceof DuplicateUserNameException) {
            return ZdNovelResponse.fail(ResponseStatus.Duplicate_User_Name);
        //拦截用户登录时输入错误的用户名
        } else if (exception instanceof UserNameException) {
            return ZdNovelResponse.fail(ResponseStatus.User_Name_Error);
        // 拦截用户登录时输入错误的用户密码
        } else if (exception instanceof UserPasswordException) {
            return ZdNovelResponse.fail(ResponseStatus.User_Password_Error);
        }
        return ZdNovelResponse.error();
    }
}
